package com.huikeportal.controller;

import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import com.huikeportal.controller.common.Lang;
import com.huikeportal.model.Language;
import com.huikeportal.model.MessageCommon;
import com.huikeportal.model.UserManagerInfo;
import com.huikeportal.model.UserInfo;
import com.huikeportal.service.CustomInfoService;
import com.huikeportal.service.LoginService;

@Controller
@Scope("prototype")
public class CustomeInfoController {

	@Autowired
	private HttpSession session;

	@Autowired
	private LoginService loginService;

	@Autowired
	private CustomInfoService customInfoService;

	@InitBinder("userManagerInfo")
	public void initBinder(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("userManagerInfo.");
	}

	@RequestMapping("/customInfo")
	public String customInfo(ModelMap model) throws Exception {
		UserInfo userInfo = (UserInfo) session.getAttribute("sessionUserName");
		String menuHtml = loginService.getMenuHtml(userInfo, "customInfo");
		UserManagerInfo userManagerInfo = customInfoService
				.getCustomeInfo(userInfo);
		model.addAttribute("menuHtml", menuHtml);// 左侧菜单html
		model.addAttribute("userManagerInfo", userManagerInfo);

		Language lang = Lang.getInfo("zh_cn");
		model.addAttribute("lang", lang);
		return "customInfo";
	}

	@RequestMapping("/customSub")
	public String customSub(UserManagerInfo userManagerInfo, ModelMap model)
			throws Exception {
		MessageCommon messageCommon = customInfoService
				.customSub(userManagerInfo);
		if (!messageCommon.isFlag()) {
			model.addAttribute("errorMsg", messageCommon.getMessage());
			return "error";
		}
		UserInfo userInfo = (UserInfo) session.getAttribute("sessionUserName");
		String menuHtml = loginService.getMenuHtml(userInfo, "customInfo");
		userManagerInfo = customInfoService.getCustomeInfo(userInfo);
		model.addAttribute("menuHtml", menuHtml);// 左侧菜单html
		model.addAttribute("userManagerInfo", userManagerInfo);
		model.addAttribute("message", messageCommon.getMessage());
		session.setAttribute("realName", userManagerInfo.getRealName());// 更新用户真实姓名
		Language lang = Lang.getInfo("zh_cn");
		model.addAttribute("lang", lang);
		return "customInfo";
	}
}
